/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.service.impl;

import me.zhengjie.domain.FhCompStaff;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.repository.FhCompStaffRepository;
import me.zhengjie.service.FhCompStaffService;
import me.zhengjie.service.dto.FhCompStaffDto;
import me.zhengjie.service.dto.FhCompStaffQueryCriteria;
import me.zhengjie.service.mapstruct.FhCompStaffMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;

import java.text.DateFormat;
import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.stream.Collectors;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author admin
* @date 2020-07-22
**/
@Service
@RequiredArgsConstructor
public class FhCompStaffServiceImpl implements FhCompStaffService {

    private final FhCompStaffRepository fhCompStaffRepository;
    private final FhCompStaffMapper fhCompStaffMapper;

    @Override
    public Map<String,Object> queryAll(FhCompStaffQueryCriteria criteria, Pageable pageable){
        Page<FhCompStaff> page = fhCompStaffRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(fhCompStaffMapper::toDto));
    }

    @Override
    public List<FhCompStaffDto> queryAll(FhCompStaffQueryCriteria criteria){
        return fhCompStaffMapper.toDto(fhCompStaffRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public FhCompStaffDto findById(Integer id) {
        FhCompStaff fhCompStaff = fhCompStaffRepository.findById(id).orElseGet(FhCompStaff::new);
        ValidationUtil.isNull(fhCompStaff.getId(),"FhCompStaff","id",id);
        return fhCompStaffMapper.toDto(fhCompStaff);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public FhCompStaffDto create(FhCompStaff resources) {
        return fhCompStaffMapper.toDto(fhCompStaffRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List totalByEducation() {
        return fhCompStaffRepository.totalByEducation();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<FhCompStaffDto> createAll(ArrayList<FhCompStaff> resources) {
        List<FhCompStaff> fhCompLists = fhCompStaffRepository.saveAll(resources);
        return fhCompLists.stream().map(fhCompStaffMapper::toDto).collect(Collectors.toList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(FhCompStaff resources) {
        FhCompStaff fhCompStaff = fhCompStaffRepository.findById(resources.getId()).orElseGet(FhCompStaff::new);
        ValidationUtil.isNull( fhCompStaff.getId(),"FhCompStaff","id",resources.getId());
        fhCompStaff.copy(resources);
        fhCompStaffRepository.save(fhCompStaff);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            fhCompStaffRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<FhCompStaffDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (FhCompStaffDto fhCompStaff : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("姓名", fhCompStaff.getName());
            map.put("性别", fhCompStaff.getSex());
            map.put("公司", fhCompStaff.getCompany().getName());
            map.put("学历", fhCompStaff.getEducation());
            map.put("电话", fhCompStaff.getPhone());
//            map.put("类型", fhCompStaff.getStaffType());
            map.put("登记时间", DateFormat.getDateInstance().format( fhCompStaff.getRegisterTime()));
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}